package GUI; //done

import java.awt.Color;
import java.awt.HeadlessException;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import Client.ConnectionToServer;
import Entities.Type;

/**
 * This Class controls Type Update Window.
 * @author waleed
 * @author kamel
 * @author ameer
 * @author bshara
 */
public class TypeUpdateWindow extends TypeManagementWindow {
	/** The serial Version UID. */
	private static final long serialVersionUID = 1L;
	
	/** The Add Flag. */
	private boolean AddFlag;
	
	/** The Update Flag. */
	private boolean UpdateFlag;
	
	/** The Remove Flag. */
	private boolean RemoveFlag;
	
	/** The Type. */
	private Type type = null;
	
	/** The Background Label. */
	private JLabel BGLabel;
	
	/**
	 * Default constructor
	 */
	public TypeUpdateWindow(){
		super();
		getSectionsIDLabel().setText("Choose Category");
		initialize();
	}

	/**
	 * This function initializes TypeUpdateWindow
	 */
	private void initialize() {
		type = new Type();
		AddFlag = false;
		UpdateFlag = false;
		RemoveFlag = false;
		getIdLabel().setText("Type ID");
		getNameLabel().setText("Type name");
		getTypesIDLabel().setText("Choose Tybe");
		getProductsIDLabel().setText("All products in this type :");
		getAddbuttonLabel().setText("Selected type info:");
		getNameTextField().setEditable(false);
		getIdTextField().setEditable(false);
		addSaveButton();
		addUpdateButton();
		addremoveButton();
		updateForm();
		getTypesIDComboBox().addActionListener(
				new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						String str = (String) getTypesIDComboBox()
								.getSelectedItem();
						if (str.equals(":"))
							return;
						String typeID = str.substring(0, str.indexOf(":"));
						getIdTextField().setText(typeID);
						getNameTextField().setText(
								str.substring(str.indexOf(":") + 1,
										str.length()));
						String[] productslist = null;
						try {
							ConnectionToServer connectiontoserver = new ConnectionToServer();
							productslist = (String[]) connectiontoserver
									.loadTypeProduct(Integer.parseInt(typeID));
							if (productslist == null) {
								getProductsIDTextArea().setText("");
							} else {
								String tmpstr = "";
								int len = productslist.length;
								for (int i = 0; i < len; i++) {
									tmpstr = tmpstr + (productslist[i] + "\n");
								}
								getProductsIDTextArea().setText(tmpstr);
							}
						} catch (Exception except) {
							JOptionPane.showMessageDialog(null,
									"SYSTEM ERROR!!!", "",
									JOptionPane.ERROR_MESSAGE);
						}
					}
				});
		BGLabel = new JLabel("");
		BGLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
		BGLabel.setBounds(-13, -471, 628, 721);
		this.add(BGLabel);	
	}

	/**
	 * This function update ComboBoxes from DB
	 */
	public void updateForm(){
		updateTypesIDComboBox();
		updateCategoriesIDComboBox();
	}

	/**
	 * This function initializes addSaveButton ActionListener
	 */
	private void addSaveButton() {
		getSaveButton().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				if (AddFlag) {
					saveType();
				} else {
					getSaveButton().setText("OK");
					getAddbuttonLabel().setText("PLZ enter type ID and Name:");
					getNameTextField().setEditable(true);
					getIdTextField().setEditable(true);
					getUpdateButton().setEnabled(false);
					getRemoveButton().setEnabled(false);
					getIdTextField().setForeground(Color.red);
					getNameTextField().setForeground(Color.red);
					getAddbuttonLabel().setForeground(Color.red);
					AddFlag = true;
				}
			}
		});
	}

	/**
	 * This function initializes addUpdateButton ActionListener
	 */
	private void addUpdateButton() {
		getUpdateButton().addActionListener(
				new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						if (UpdateFlag) {
							updateType();
						} else {
							getUpdateButton().setText("OK");
							getAddbuttonLabel().setText(
									"PLZ enter new type name:");
							getNameTextField().setEditable(true);
							getRemoveButton().setEnabled(false);
							getSaveButton().setEnabled(false);
							getIdTextField().setForeground(Color.red);
							getNameTextField().setForeground(Color.red);
							getAddbuttonLabel().setForeground(Color.red);
							UpdateFlag = true;
						}
					}
				});
	}

	/**
	 * This function update selected Type
	 */
	private void updateType() {
		int typeID = 0;
		try {
			typeID = Integer.parseInt(getIdTextField().getText());
		} catch (NumberFormatException exception) {
			return;
		}
		String typeName = getNameTextField().getText();
		if (typeID > 0 && !typeName.equals("")) {
			type.setTypeID(typeID);
			type.setTypeName(typeName);
			try {
				if (type.updateType()) {
					String oldname = (String) getTypesIDComboBox()
							.getSelectedItem();
					oldname = oldname.substring(oldname.indexOf(":") + 1,
							oldname.length());
					getTypesIDComboBox().removeItem(typeID + ":" + oldname);
					getTypesIDComboBox().addItem(typeID + ":" + typeName);
					JOptionPane.showMessageDialog(null,
							"Type successfuly update");
					restore();
				} else
					JOptionPane.showMessageDialog(null, "Type not update!!!",
							"", JOptionPane.WARNING_MESSAGE);
			} catch (Exception e1) {
				JOptionPane.showMessageDialog(null, "Error!", "",
						JOptionPane.ERROR_MESSAGE);
			}
		} else
			JOptionPane.showMessageDialog(null, "PLZ enter type name!", "",
					JOptionPane.WARNING_MESSAGE);
	}

	/**
	 * This function add entered type to data base
	 */
	private void saveType(){
		int typeID = 0;
		try {
			typeID = Integer.parseInt(getIdTextField().getText());
		} catch (NumberFormatException exception) {
			JOptionPane.showMessageDialog(null, "Ilegal ID", "",
					JOptionPane.WARNING_MESSAGE);
			return;
		}
		String typeName = getNameTextField().getText();
		if (typeID > 0 && !typeName.equals("")) {
			type.setTypeID(typeID);
			type.setTypeName(typeName);
			try {
				if (type.addType()) {
					JOptionPane.showMessageDialog(null,
							"Type successfuly saved");
					getTypesIDComboBox().addItem(typeID + ":" + typeName);
					restore();
				} else
					JOptionPane.showMessageDialog(null, "Type not saved!!!",
							"", JOptionPane.WARNING_MESSAGE);
			} catch (Exception e1) {
				JOptionPane.showMessageDialog(null, "Error!", "",
						JOptionPane.ERROR_MESSAGE);
			}
		} else
			JOptionPane.showMessageDialog(null, "Ilegal input!", "",
					JOptionPane.WARNING_MESSAGE);
	}

	/**
	 * This function initializes addremoveButton ActionListener
	 */
	private void addremoveButton(){
		getRemoveButton().addActionListener(
				new java.awt.event.ActionListener(){
					public void actionPerformed(java.awt.event.ActionEvent e) {
						if (RemoveFlag){
							removeType();
							restore();
						} 
						else {
							getRemoveButton().setText("OK");
							getAddbuttonLabel().setText("Remove this type:");
							getUpdateButton().setEnabled(false);
							getSaveButton().setEnabled(false);
							getIdTextField().setForeground(Color.red);
							getNameTextField().setForeground(Color.red);
							getAddbuttonLabel().setForeground(Color.red);
							RemoveFlag = true;
						}
					}
				});
	}

	/**
	 * This function remove selected type from data base
	 */
	private void removeType(){
		int typeID = 0;
		try{
			typeID = Integer.parseInt(getIdTextField().getText());
		} 
		catch (NumberFormatException exception){
			return;
		}

		type.setTypeID(typeID);
		try {
			if (type.removeType()){
				getTypesIDComboBox().removeItem(
						typeID + ":" + getNameTextField().getText());
				JOptionPane.showMessageDialog(null, "Type successfuly remove");
			} 
			else
				JOptionPane.showMessageDialog(null, "Type not remove!!!", "",
						JOptionPane.WARNING_MESSAGE);
		} 
		catch (HeadlessException e1) {
			JOptionPane.showMessageDialog(null, "System ERROR!!!", "",
					JOptionPane.ERROR_MESSAGE);

		} 
		catch (Exception e1){
			JOptionPane.showMessageDialog(null, "Error!", "",
					JOptionPane.ERROR_MESSAGE);
		}
		;
	}

	/**
	 * This function restore the form button
	 */
	public void restore() {
		getSaveButton().setText("Add new");
		getUpdateButton().setText("UPDATE");
		getRemoveButton().setText("Remove");
		getAddbuttonLabel().setText("Selected type info:");
		AddFlag = false;
		UpdateFlag = false;
		RemoveFlag = false;
		getIdTextField().setForeground(Color.black);
		getNameTextField().setForeground(Color.black);
		getAddbuttonLabel().setForeground(Color.black);
		getNameTextField().setEditable(false);
		getIdTextField().setEditable(false);
		getIdTextField().setText("");
		getNameTextField().setText("");
		getUpdateButton().setEnabled(true);
		getRemoveButton().setEnabled(true);
		getSaveButton().setEnabled(true);
	}
}
